package org.example.sorted;

import java.util.Arrays;

/**
 * @Author: zjg
 * @Date: 2024/1/30 14:18
 * @Description: 冒泡排
 **/
public class MaoPao {

    private static final int [] arr={11,33,4,2,6,5,98};

    /**
     * @Author: zjg
     * @Date: 2024/1/31 14:31
     * @Description: 冒泡排序
     **/
    public static void selectorSorted(){
        for (int i = 0; i <arr.length ; i++) {
            for (int j = i+1; j <arr.length ; j++) {
                if(arr[i]>arr[j]){
                    CommonUtils.swap(arr,i,j);
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    // 优化
    public static void selectorSortedPlus(){
        if(arr==null || arr.length<2){
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            for (int j = i-1; j >=0 && arr[j]>arr[j+1]; j--) {
                CommonUtils.swap(arr,j,j+1);
            }
        }
        System.out.println(Arrays.toString(arr));
    }

    public static void main(String[] args) {
        selectorSortedPlus();
    }
}
